﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS.BUS;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

namespace NangLuongTieuDiemGUI
{
    public partial class frmNhapThietBiDien : Form
    {
        ThietBiDienBUS tbdBus = new ThietBiDienBUS();
        DataTable dt = new DataTable();
        int irun = 1;
        public frmNhapThietBiDien()
        {
            InitializeComponent();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void frmNhapThietBiDien_Load(object sender, EventArgs e)
        {
            DataColumn column;
            column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "STT";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.Int32");
            column.ColumnName = "ID";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = Type.GetType("System.String");
            column.ColumnName = "TenThietBiDien";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "CongSuat";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "CongSuatHaoHut";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Double");
            column.ColumnName = "HeSoDeBa";
            dt.Columns.Add(column);

            DataRow row;
            foreach (ThietBiDienDTO tbdDto in tbdBus.GetAll())
            {
                row = dt.NewRow();
                row["STT"] = irun;
                row["ID"] = tbdDto.ID;
                row["TenThietBiDien"] = tbdDto.TenThietBiDien;
                row["CongSuat"] = tbdDto.CongSuat;
                row["CongSuatHaoHut"] = tbdDto.CongSuatHaoHut;
                row["HeSoDeBa"] = tbdDto.HeSoDeBa;
                dt.Rows.Add(row);
                dt.AcceptChanges();
                irun++;
            }
            gridControl1.DataSource = dt;
        }

        private void ButtonStatus(Boolean status)
        {
            msEdit.Enabled = status;
            msSave.Enabled = msCancel.Enabled = !status;

            gridView1.OptionsBehavior.AllowAddRows = DefaultBoolean.True;
            gridView1.OptionsBehavior.AllowDeleteRows = DefaultBoolean.True;
            gridView1.OptionsBehavior.Editable = !status;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom; 
        }

        private void msEdit_Click(object sender, EventArgs e)
        {
            ButtonStatus(false);
        }

        private void msSave_Click(object sender, EventArgs e)
        {
            ThietBiDienDTO tbdDto = new ThietBiDienDTO();
            //try
            //{
                foreach (DataRow row in dt.Rows)
                {
                    tbdDto.TenThietBiDien = row["TenThietBiDien"].ToString();
                    tbdDto.CongSuat = Convert.ToDouble(row["CongSuat"]);
                    tbdDto.CongSuatHaoHut = Convert.ToDouble(row["CongSuatHaoHut"]);
                    tbdDto.HeSoDeBa = Convert.ToDouble(row["HeSoDeBa"]);
                    tbdDto.Xoa = false;
                    tbdDto.GhiChu = "N/A"; //sau nay sua lai phai them cot ghi chu
                    switch (row.RowState)
                    {
                        case DataRowState.Added:
                            row["ID"] = tbdBus.insert(tbdDto);
                            break;
                        case DataRowState.Modified:
                            tbdBus.update(tbdDto);
                            break;
                        case DataRowState.Deleted:
                            //tbdDto.Xoa = true;// Xoa tam thoi
                            //tbdBus.update(tbdDto); 
                            tbdBus.Delete(Convert.ToInt32(row["ID"]));//Xoa luon
                            break;
                        default: break;
                    }
                }
                dt.AcceptChanges();
                ButtonStatus(true);
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("Bạn phải điền hết vào các ô mới save được.", "Thong bao:");
            //}
        }

        private void msCancel_Click(object sender, EventArgs e)
        {
            ButtonStatus(true);
            gridControl1.DataSource = dt;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None; 
        }

        private void gridView1_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            //event khi tao 1 dong moi tren gridview thi them STT
            ColumnView View = sender as ColumnView;
            View.SetRowCellValue(e.RowHandle, View.Columns["STT"],irun);
            irun++;

            //load new ID vao gridview
            View.SetRowCellValue(e.RowHandle, View.Columns["ID"],tbdBus.SelectMaxId()+1);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            //event xoa 1 dong tren gridView
            if (e.KeyCode == Keys.Delete)
            {
                this.gridView1.DeleteRow(this.gridView1.FocusedRowHandle);
            }
        }
    }
}
